{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pandas-重采样及频率转换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "内容介绍:重采样是指将时间序列从一个频率转换到另一个频率的处理过程。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "分类:\n",
    "\n",
    "* 降采样：将高频率数据聚合到低频率(降频率采样，例如将月度数据转换为季度数据)\n",
    "* 升采样：将低频率数据转换为高频率（例如将季度数据转换为月度数据）\n",
    "* 其他采样。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "重采样主要使用resample函数：\n",
    "\n",
    "resample(self, rule, axis=0, closed: 'Optional[str]' = None, label: 'Optional[str]' = None, convention: 'str' = 'start', kind: 'Optional[str]' = None, loffset=None, base: 'Optional[int]' = None, on=None, level=None, origin: 'Union[str, TimestampConvertibleTypes]' = 'start_day', offset: 'Optional[TimedeltaConvertibleTypes]' = None) -> 'Resampler'\n",
    "\n",
    "* rule:重采样规则\n",
    "* axis:采样数据的轴，默认为0轴\n",
    "* fill_method:升采样如何插值，如ffill,bfill。默认不插值。\n",
    "* ohlc:open,high,low,close四种采样方式\n",
    "* 其他参数详见：help(pd.DataFrame.resample)\n",
    "\n",
    "* how:重采样后以何种方式聚合。以类似于resampe().mean()的形式使用。\n",
    "**默认是求平均值，即参数为mean。也可以是first,last,max,min。**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2018-01-01    0.553903\n",
       "2018-01-02   -0.034592\n",
       "2018-01-03   -0.381743\n",
       "2018-01-04   -0.531937\n",
       "2018-01-05   -1.739332\n",
       "                ...   \n",
       "2018-04-06    1.862778\n",
       "2018-04-07   -1.581180\n",
       "2018-04-08    0.055110\n",
       "2018-04-09    1.401881\n",
       "2018-04-10   -0.986895\n",
       "Freq: D, Length: 100, dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 示例数据\n",
    "rng = pd.date_range('2018-1-1',periods=100,freq='D')\n",
    "ts = pd.Series(np.random.randn(len(rng)),index=rng)\n",
    "ts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.resample基本方法的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2018-01-31   -0.014873\n",
       "2018-02-28    0.396786\n",
       "2018-03-31   -0.164874\n",
       "2018-04-30    0.575431\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 日频率转换为月频率\n",
    "ts.resample('M').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2018-01   -0.014873\n",
       "2018-02    0.396786\n",
       "2018-03   -0.164874\n",
       "2018-04    0.575431\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换为时期频率\n",
    "ts.resample('M',kind='period').mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.降采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-01-01 00:00:00     0\n",
       "2019-01-01 00:01:00     1\n",
       "2019-01-01 00:02:00     2\n",
       "2019-01-01 00:03:00     3\n",
       "2019-01-01 00:04:00     4\n",
       "2019-01-01 00:05:00     5\n",
       "2019-01-01 00:06:00     6\n",
       "2019-01-01 00:07:00     7\n",
       "2019-01-01 00:08:00     8\n",
       "2019-01-01 00:09:00     9\n",
       "2019-01-01 00:10:00    10\n",
       "2019-01-01 00:11:00    11\n",
       "Freq: T, dtype: int64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 日期范围，频率1分钟\n",
    "rng2 = pd.date_range('2019-1-1',periods=12,freq='T')\n",
    "ts2=pd.Series(np.arange(12),index=rng2)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2018-12-31 23:55:00    1\n",
       "2019-01-01 00:00:00    5\n",
       "2019-01-01 00:05:00    5\n",
       "2019-01-01 00:10:00    1\n",
       "Freq: 5T, dtype: int64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过求和的方式，将数据聚合到5分钟的块中\n",
    "# 包含面元的右边界，最终的时间序列是以各面元边界的时间戳进行标记的\n",
    "# 多一个区间(2018-12-31 23:55:00,2019-01-01 00:00:00)。\n",
    "# 2019-01-01 00:00:00落不到原来的第一个区间里,所以要往前补足。\n",
    "ts2.resample('5min',closed='right').count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-01-01 00:00:00    10\n",
       "2019-01-01 00:05:00    35\n",
       "2019-01-01 00:10:00    21\n",
       "Freq: 5T, dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#当参数close=left时，包含面元的左边界\n",
    "ts2.resample('5min',closed='left').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-01-01 00:00:00     0\n",
       "2019-01-01 00:05:00    15\n",
       "2019-01-01 00:10:00    40\n",
       "2019-01-01 00:15:00    11\n",
       "Freq: 5T, dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#当参数close=left时，包含面元的左边界\n",
    "ts2.resample('5min',closed='right',label='right').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-16-457e5da0b121>:2: FutureWarning: 'loffset' in .resample() and in Grouper() is deprecated.\n",
      "\n",
      ">>> df.resample(freq=\"3s\", loffset=\"8H\")\n",
      "\n",
      "becomes:\n",
      "\n",
      ">>> from pandas.tseries.frequencies import to_offset\n",
      ">>> df = df.resample(freq=\"3s\").mean()\n",
      ">>> df.index = df.index.to_timestamp() + to_offset(\"8H\")\n",
      "\n",
      "  ts.resample('5min',closed='right',label='right',loffset='-1s').sum()\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2018-12-31 23:59:59     0\n",
       "2019-01-01 00:04:59    15\n",
       "2019-01-01 00:09:59    40\n",
       "2019-01-01 00:14:59    11\n",
       "Freq: 5T, dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#当参数close=left时，包含面元的左边界。\n",
    "ts2.resample('5min',closed='right',label='right',loffset='-1s').sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.OHLC采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-01-01 00:00:00</th>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 00:05:00</th>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-01 00:10:00</th>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     open  high  low  close\n",
       "2019-01-01 00:00:00     0     4    0      4\n",
       "2019-01-01 00:05:00     5     9    5      9\n",
       "2019-01-01 00:10:00    10    11   10     11"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts2.resample('5min').ohlc()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.升采样和插值\n",
    "\n",
    "* 将数据从低频率转换为高频率不需要聚合\n",
    "* 使用asfreq方法转换为高频，则不经过聚合\n",
    "* 使用resample的参数实现填充和插值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 使用时期索引的数据进行重采样，与时间戳相似\n",
    "* 因为时期指的是时间区间，所以升采样和降采样的规则比较严格\n",
    "\n",
    "**基本规则：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>广州</th>\n",
       "      <th>上海</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-01-06</th>\n",
       "      <td>0.859810</td>\n",
       "      <td>0.990167</td>\n",
       "      <td>2.056032</td>\n",
       "      <td>-1.942216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-13</th>\n",
       "      <td>0.802784</td>\n",
       "      <td>0.959297</td>\n",
       "      <td>-0.440185</td>\n",
       "      <td>-1.049184</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  北京        广州        上海        深圳\n",
       "2021-01-06  0.859810  0.990167  2.056032 -1.942216\n",
       "2021-01-13  0.802784  0.959297 -0.440185 -1.049184"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4 = pd.DataFrame(np.random.randn(2,4),\n",
    "                  index=pd.date_range('1/1/2021',periods=2,freq='W-WED'),\n",
    "                  columns=['北京','广州','上海','深圳']\n",
    "                  )\n",
    "df4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>广州</th>\n",
       "      <th>上海</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-01-06</th>\n",
       "      <td>0.859810</td>\n",
       "      <td>0.990167</td>\n",
       "      <td>2.056032</td>\n",
       "      <td>-1.942216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-07</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-08</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-09</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-10</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-11</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-12</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-13</th>\n",
       "      <td>0.802784</td>\n",
       "      <td>0.959297</td>\n",
       "      <td>-0.440185</td>\n",
       "      <td>-1.049184</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  北京        广州        上海        深圳\n",
       "2021-01-06  0.859810  0.990167  2.056032 -1.942216\n",
       "2021-01-07       NaN       NaN       NaN       NaN\n",
       "2021-01-08       NaN       NaN       NaN       NaN\n",
       "2021-01-09       NaN       NaN       NaN       NaN\n",
       "2021-01-10       NaN       NaN       NaN       NaN\n",
       "2021-01-11       NaN       NaN       NaN       NaN\n",
       "2021-01-12       NaN       NaN       NaN       NaN\n",
       "2021-01-13  0.802784  0.959297 -0.440185 -1.049184"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据从低频率到高频率不需要聚合，但是会出现缺失值\n",
    "# 使用asfreq()方法转换成高频，则不经过聚合\n",
    "df_daily = df4.resample('D').asfreq()\n",
    "df_daily"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>广州</th>\n",
       "      <th>上海</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-01-06</th>\n",
       "      <td>0.859810</td>\n",
       "      <td>0.990167</td>\n",
       "      <td>2.056032</td>\n",
       "      <td>-1.942216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-07</th>\n",
       "      <td>0.859810</td>\n",
       "      <td>0.990167</td>\n",
       "      <td>2.056032</td>\n",
       "      <td>-1.942216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-08</th>\n",
       "      <td>0.859810</td>\n",
       "      <td>0.990167</td>\n",
       "      <td>2.056032</td>\n",
       "      <td>-1.942216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-09</th>\n",
       "      <td>0.859810</td>\n",
       "      <td>0.990167</td>\n",
       "      <td>2.056032</td>\n",
       "      <td>-1.942216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-10</th>\n",
       "      <td>0.859810</td>\n",
       "      <td>0.990167</td>\n",
       "      <td>2.056032</td>\n",
       "      <td>-1.942216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-11</th>\n",
       "      <td>0.859810</td>\n",
       "      <td>0.990167</td>\n",
       "      <td>2.056032</td>\n",
       "      <td>-1.942216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-12</th>\n",
       "      <td>0.859810</td>\n",
       "      <td>0.990167</td>\n",
       "      <td>2.056032</td>\n",
       "      <td>-1.942216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-13</th>\n",
       "      <td>0.802784</td>\n",
       "      <td>0.959297</td>\n",
       "      <td>-0.440185</td>\n",
       "      <td>-1.049184</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  北京        广州        上海        深圳\n",
       "2021-01-06  0.859810  0.990167  2.056032 -1.942216\n",
       "2021-01-07  0.859810  0.990167  2.056032 -1.942216\n",
       "2021-01-08  0.859810  0.990167  2.056032 -1.942216\n",
       "2021-01-09  0.859810  0.990167  2.056032 -1.942216\n",
       "2021-01-10  0.859810  0.990167  2.056032 -1.942216\n",
       "2021-01-11  0.859810  0.990167  2.056032 -1.942216\n",
       "2021-01-12  0.859810  0.990167  2.056032 -1.942216\n",
       "2021-01-13  0.802784  0.959297 -0.440185 -1.049184"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用resampleing的参数实现填充和插值\n",
    "df4.resample('D').ffill()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>广州</th>\n",
       "      <th>上海</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-01-06</th>\n",
       "      <td>0.859810</td>\n",
       "      <td>0.990167</td>\n",
       "      <td>2.056032</td>\n",
       "      <td>-1.942216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-07</th>\n",
       "      <td>0.859810</td>\n",
       "      <td>0.990167</td>\n",
       "      <td>2.056032</td>\n",
       "      <td>-1.942216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-08</th>\n",
       "      <td>0.859810</td>\n",
       "      <td>0.990167</td>\n",
       "      <td>2.056032</td>\n",
       "      <td>-1.942216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-09</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-10</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-11</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-12</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-13</th>\n",
       "      <td>0.802784</td>\n",
       "      <td>0.959297</td>\n",
       "      <td>-0.440185</td>\n",
       "      <td>-1.049184</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  北京        广州        上海        深圳\n",
       "2021-01-06  0.859810  0.990167  2.056032 -1.942216\n",
       "2021-01-07  0.859810  0.990167  2.056032 -1.942216\n",
       "2021-01-08  0.859810  0.990167  2.056032 -1.942216\n",
       "2021-01-09       NaN       NaN       NaN       NaN\n",
       "2021-01-10       NaN       NaN       NaN       NaN\n",
       "2021-01-11       NaN       NaN       NaN       NaN\n",
       "2021-01-12       NaN       NaN       NaN       NaN\n",
       "2021-01-13  0.802784  0.959297 -0.440185 -1.049184"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 填充部分\n",
    "df4.resample('D').ffill(limit=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>广州</th>\n",
       "      <th>上海</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-01-07</th>\n",
       "      <td>0.859810</td>\n",
       "      <td>0.990167</td>\n",
       "      <td>2.056032</td>\n",
       "      <td>-1.942216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-14</th>\n",
       "      <td>0.802784</td>\n",
       "      <td>0.959297</td>\n",
       "      <td>-0.440185</td>\n",
       "      <td>-1.049184</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  北京        广州        上海        深圳\n",
       "2021-01-07  0.859810  0.990167  2.056032 -1.942216\n",
       "2021-01-14  0.802784  0.959297 -0.440185 -1.049184"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 变换频率，星期三转换为星期四\n",
    "df4.resample('W-THU').ffill()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.通过时期进行重采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>广州</th>\n",
       "      <th>上海</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01</th>\n",
       "      <td>-0.701865</td>\n",
       "      <td>0.541006</td>\n",
       "      <td>0.423853</td>\n",
       "      <td>0.300178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-02</th>\n",
       "      <td>0.759198</td>\n",
       "      <td>-1.276694</td>\n",
       "      <td>0.869328</td>\n",
       "      <td>2.283170</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-03</th>\n",
       "      <td>-0.335607</td>\n",
       "      <td>0.437015</td>\n",
       "      <td>-0.269833</td>\n",
       "      <td>1.322394</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-04</th>\n",
       "      <td>0.635993</td>\n",
       "      <td>0.138992</td>\n",
       "      <td>-2.301968</td>\n",
       "      <td>-1.284577</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05</th>\n",
       "      <td>-0.072213</td>\n",
       "      <td>0.140781</td>\n",
       "      <td>-1.941532</td>\n",
       "      <td>-0.347236</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               北京        广州        上海        深圳\n",
       "2020-01 -0.701865  0.541006  0.423853  0.300178\n",
       "2020-02  0.759198 -1.276694  0.869328  2.283170\n",
       "2020-03 -0.335607  0.437015 -0.269833  1.322394\n",
       "2020-04  0.635993  0.138992 -2.301968 -1.284577\n",
       "2020-05 -0.072213  0.140781 -1.941532 -0.347236"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df5 = pd.DataFrame(np.random.randn(24,4),\n",
    "                  index=pd.period_range('1-2020','12-2021',freq='M'),\n",
    "                  columns=['北京','广州','上海','深圳']\n",
    "                  )\n",
    "df5.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>广州</th>\n",
       "      <th>上海</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020</th>\n",
       "      <td>0.041654</td>\n",
       "      <td>-0.609221</td>\n",
       "      <td>-0.334578</td>\n",
       "      <td>0.306583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021</th>\n",
       "      <td>-0.123737</td>\n",
       "      <td>-0.223495</td>\n",
       "      <td>0.629238</td>\n",
       "      <td>0.060333</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            北京        广州        上海        深圳\n",
       "2020  0.041654 -0.609221 -0.334578  0.306583\n",
       "2021 -0.123737 -0.223495  0.629238  0.060333"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用时期索引的数据进行重采样，与时间戳相似\n",
    "annual_df5 = df5.resample('A-DEC').mean()\n",
    "annual_df5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>广州</th>\n",
       "      <th>上海</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020Q1</th>\n",
       "      <td>0.041654</td>\n",
       "      <td>-0.609221</td>\n",
       "      <td>-0.334578</td>\n",
       "      <td>0.306583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020Q2</th>\n",
       "      <td>0.041654</td>\n",
       "      <td>-0.609221</td>\n",
       "      <td>-0.334578</td>\n",
       "      <td>0.306583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020Q3</th>\n",
       "      <td>0.041654</td>\n",
       "      <td>-0.609221</td>\n",
       "      <td>-0.334578</td>\n",
       "      <td>0.306583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020Q4</th>\n",
       "      <td>0.041654</td>\n",
       "      <td>-0.609221</td>\n",
       "      <td>-0.334578</td>\n",
       "      <td>0.306583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021Q1</th>\n",
       "      <td>-0.123737</td>\n",
       "      <td>-0.223495</td>\n",
       "      <td>0.629238</td>\n",
       "      <td>0.060333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021Q2</th>\n",
       "      <td>-0.123737</td>\n",
       "      <td>-0.223495</td>\n",
       "      <td>0.629238</td>\n",
       "      <td>0.060333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021Q3</th>\n",
       "      <td>-0.123737</td>\n",
       "      <td>-0.223495</td>\n",
       "      <td>0.629238</td>\n",
       "      <td>0.060333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021Q4</th>\n",
       "      <td>-0.123737</td>\n",
       "      <td>-0.223495</td>\n",
       "      <td>0.629238</td>\n",
       "      <td>0.060333</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              北京        广州        上海        深圳\n",
       "2020Q1  0.041654 -0.609221 -0.334578  0.306583\n",
       "2020Q2  0.041654 -0.609221 -0.334578  0.306583\n",
       "2020Q3  0.041654 -0.609221 -0.334578  0.306583\n",
       "2020Q4  0.041654 -0.609221 -0.334578  0.306583\n",
       "2021Q1 -0.123737 -0.223495  0.629238  0.060333\n",
       "2021Q2 -0.123737 -0.223495  0.629238  0.060333\n",
       "2021Q3 -0.123737 -0.223495  0.629238  0.060333\n",
       "2021Q4 -0.123737 -0.223495  0.629238  0.060333"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "annual_df5.resample('Q-DEC').ffill()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>广州</th>\n",
       "      <th>上海</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020Q4</th>\n",
       "      <td>0.041654</td>\n",
       "      <td>-0.609221</td>\n",
       "      <td>-0.334578</td>\n",
       "      <td>0.306583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021Q1</th>\n",
       "      <td>0.041654</td>\n",
       "      <td>-0.609221</td>\n",
       "      <td>-0.334578</td>\n",
       "      <td>0.306583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021Q2</th>\n",
       "      <td>0.041654</td>\n",
       "      <td>-0.609221</td>\n",
       "      <td>-0.334578</td>\n",
       "      <td>0.306583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021Q3</th>\n",
       "      <td>0.041654</td>\n",
       "      <td>-0.609221</td>\n",
       "      <td>-0.334578</td>\n",
       "      <td>0.306583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021Q4</th>\n",
       "      <td>-0.123737</td>\n",
       "      <td>-0.223495</td>\n",
       "      <td>0.629238</td>\n",
       "      <td>0.060333</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              北京        广州        上海        深圳\n",
       "2020Q4  0.041654 -0.609221 -0.334578  0.306583\n",
       "2021Q1  0.041654 -0.609221 -0.334578  0.306583\n",
       "2021Q2  0.041654 -0.609221 -0.334578  0.306583\n",
       "2021Q3  0.041654 -0.609221 -0.334578  0.306583\n",
       "2021Q4 -0.123737 -0.223495  0.629238  0.060333"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#convention='end'以原数据的季度中最后一个填充\n",
    "annual_df5.resample('Q-DEC',convention='end').ffill()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>广州</th>\n",
       "      <th>上海</th>\n",
       "      <th>深圳</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020Q4</th>\n",
       "      <td>0.041654</td>\n",
       "      <td>-0.609221</td>\n",
       "      <td>-0.334578</td>\n",
       "      <td>0.306583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021Q1</th>\n",
       "      <td>0.041654</td>\n",
       "      <td>-0.609221</td>\n",
       "      <td>-0.334578</td>\n",
       "      <td>0.306583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021Q2</th>\n",
       "      <td>0.041654</td>\n",
       "      <td>-0.609221</td>\n",
       "      <td>-0.334578</td>\n",
       "      <td>0.306583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021Q3</th>\n",
       "      <td>0.041654</td>\n",
       "      <td>-0.609221</td>\n",
       "      <td>-0.334578</td>\n",
       "      <td>0.306583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021Q4</th>\n",
       "      <td>-0.123737</td>\n",
       "      <td>-0.223495</td>\n",
       "      <td>0.629238</td>\n",
       "      <td>0.060333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q1</th>\n",
       "      <td>-0.123737</td>\n",
       "      <td>-0.223495</td>\n",
       "      <td>0.629238</td>\n",
       "      <td>0.060333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q2</th>\n",
       "      <td>-0.123737</td>\n",
       "      <td>-0.223495</td>\n",
       "      <td>0.629238</td>\n",
       "      <td>0.060333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q3</th>\n",
       "      <td>-0.123737</td>\n",
       "      <td>-0.223495</td>\n",
       "      <td>0.629238</td>\n",
       "      <td>0.060333</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              北京        广州        上海        深圳\n",
       "2020Q4  0.041654 -0.609221 -0.334578  0.306583\n",
       "2021Q1  0.041654 -0.609221 -0.334578  0.306583\n",
       "2021Q2  0.041654 -0.609221 -0.334578  0.306583\n",
       "2021Q3  0.041654 -0.609221 -0.334578  0.306583\n",
       "2021Q4 -0.123737 -0.223495  0.629238  0.060333\n",
       "2022Q1 -0.123737 -0.223495  0.629238  0.060333\n",
       "2022Q2 -0.123737 -0.223495  0.629238  0.060333\n",
       "2022Q3 -0.123737 -0.223495  0.629238  0.060333"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "annual_df5.resample('Q-MAR').ffill()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function resample in module pandas.core.generic:\n",
      "\n",
      "resample(self, rule, axis=0, closed: 'Optional[str]' = None, label: 'Optional[str]' = None, convention: 'str' = 'start', kind: 'Optional[str]' = None, loffset=None, base: 'Optional[int]' = None, on=None, level=None, origin: 'Union[str, TimestampConvertibleTypes]' = 'start_day', offset: 'Optional[TimedeltaConvertibleTypes]' = None) -> 'Resampler'\n",
      "    Resample time-series data.\n",
      "    \n",
      "    Convenience method for frequency conversion and resampling of time\n",
      "    series. Object must have a datetime-like index (`DatetimeIndex`,\n",
      "    `PeriodIndex`, or `TimedeltaIndex`), or pass datetime-like values\n",
      "    to the `on` or `level` keyword.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    rule : DateOffset, Timedelta or str\n",
      "        The offset string or object representing target conversion.\n",
      "    axis : {0 or 'index', 1 or 'columns'}, default 0\n",
      "        Which axis to use for up- or down-sampling. For `Series` this\n",
      "        will default to 0, i.e. along the rows. Must be\n",
      "        `DatetimeIndex`, `TimedeltaIndex` or `PeriodIndex`.\n",
      "    closed : {'right', 'left'}, default None\n",
      "        Which side of bin interval is closed. The default is 'left'\n",
      "        for all frequency offsets except for 'M', 'A', 'Q', 'BM',\n",
      "        'BA', 'BQ', and 'W' which all have a default of 'right'.\n",
      "    label : {'right', 'left'}, default None\n",
      "        Which bin edge label to label bucket with. The default is 'left'\n",
      "        for all frequency offsets except for 'M', 'A', 'Q', 'BM',\n",
      "        'BA', 'BQ', and 'W' which all have a default of 'right'.\n",
      "    convention : {'start', 'end', 's', 'e'}, default 'start'\n",
      "        For `PeriodIndex` only, controls whether to use the start or\n",
      "        end of `rule`.\n",
      "    kind : {'timestamp', 'period'}, optional, default None\n",
      "        Pass 'timestamp' to convert the resulting index to a\n",
      "        `DateTimeIndex` or 'period' to convert it to a `PeriodIndex`.\n",
      "        By default the input representation is retained.\n",
      "    loffset : timedelta, default None\n",
      "        Adjust the resampled time labels.\n",
      "    \n",
      "        .. deprecated:: 1.1.0\n",
      "            You should add the loffset to the `df.index` after the resample.\n",
      "            See below.\n",
      "    \n",
      "    base : int, default 0\n",
      "        For frequencies that evenly subdivide 1 day, the \"origin\" of the\n",
      "        aggregated intervals. For example, for '5min' frequency, base could\n",
      "        range from 0 through 4. Defaults to 0.\n",
      "    \n",
      "        .. deprecated:: 1.1.0\n",
      "            The new arguments that you should use are 'offset' or 'origin'.\n",
      "    \n",
      "    on : str, optional\n",
      "        For a DataFrame, column to use instead of index for resampling.\n",
      "        Column must be datetime-like.\n",
      "    level : str or int, optional\n",
      "        For a MultiIndex, level (name or number) to use for\n",
      "        resampling. `level` must be datetime-like.\n",
      "    origin : {'epoch', 'start', 'start_day'}, Timestamp or str, default 'start_day'\n",
      "        The timestamp on which to adjust the grouping. The timezone of origin\n",
      "        must match the timezone of the index.\n",
      "        If a timestamp is not used, these values are also supported:\n",
      "    \n",
      "        - 'epoch': `origin` is 1970-01-01\n",
      "        - 'start': `origin` is the first value of the timeseries\n",
      "        - 'start_day': `origin` is the first day at midnight of the timeseries\n",
      "    \n",
      "        .. versionadded:: 1.1.0\n",
      "    \n",
      "    offset : Timedelta or str, default is None\n",
      "        An offset timedelta added to the origin.\n",
      "    \n",
      "        .. versionadded:: 1.1.0\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    Resampler object\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    groupby : Group by mapping, function, label, or list of labels.\n",
      "    Series.resample : Resample a Series.\n",
      "    DataFrame.resample: Resample a DataFrame.\n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    See the `user guide\n",
      "    <https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#resampling>`_\n",
      "    for more.\n",
      "    \n",
      "    To learn more about the offset strings, please see `this link\n",
      "    <https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#dateoffset-objects>`__.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    Start by creating a series with 9 one minute timestamps.\n",
      "    \n",
      "    >>> index = pd.date_range('1/1/2000', periods=9, freq='T')\n",
      "    >>> series = pd.Series(range(9), index=index)\n",
      "    >>> series\n",
      "    2000-01-01 00:00:00    0\n",
      "    2000-01-01 00:01:00    1\n",
      "    2000-01-01 00:02:00    2\n",
      "    2000-01-01 00:03:00    3\n",
      "    2000-01-01 00:04:00    4\n",
      "    2000-01-01 00:05:00    5\n",
      "    2000-01-01 00:06:00    6\n",
      "    2000-01-01 00:07:00    7\n",
      "    2000-01-01 00:08:00    8\n",
      "    Freq: T, dtype: int64\n",
      "    \n",
      "    Downsample the series into 3 minute bins and sum the values\n",
      "    of the timestamps falling into a bin.\n",
      "    \n",
      "    >>> series.resample('3T').sum()\n",
      "    2000-01-01 00:00:00     3\n",
      "    2000-01-01 00:03:00    12\n",
      "    2000-01-01 00:06:00    21\n",
      "    Freq: 3T, dtype: int64\n",
      "    \n",
      "    Downsample the series into 3 minute bins as above, but label each\n",
      "    bin using the right edge instead of the left. Please note that the\n",
      "    value in the bucket used as the label is not included in the bucket,\n",
      "    which it labels. For example, in the original series the\n",
      "    bucket ``2000-01-01 00:03:00`` contains the value 3, but the summed\n",
      "    value in the resampled bucket with the label ``2000-01-01 00:03:00``\n",
      "    does not include 3 (if it did, the summed value would be 6, not 3).\n",
      "    To include this value close the right side of the bin interval as\n",
      "    illustrated in the example below this one.\n",
      "    \n",
      "    >>> series.resample('3T', label='right').sum()\n",
      "    2000-01-01 00:03:00     3\n",
      "    2000-01-01 00:06:00    12\n",
      "    2000-01-01 00:09:00    21\n",
      "    Freq: 3T, dtype: int64\n",
      "    \n",
      "    Downsample the series into 3 minute bins as above, but close the right\n",
      "    side of the bin interval.\n",
      "    \n",
      "    >>> series.resample('3T', label='right', closed='right').sum()\n",
      "    2000-01-01 00:00:00     0\n",
      "    2000-01-01 00:03:00     6\n",
      "    2000-01-01 00:06:00    15\n",
      "    2000-01-01 00:09:00    15\n",
      "    Freq: 3T, dtype: int64\n",
      "    \n",
      "    Upsample the series into 30 second bins.\n",
      "    \n",
      "    >>> series.resample('30S').asfreq()[0:5]   # Select first 5 rows\n",
      "    2000-01-01 00:00:00   0.0\n",
      "    2000-01-01 00:00:30   NaN\n",
      "    2000-01-01 00:01:00   1.0\n",
      "    2000-01-01 00:01:30   NaN\n",
      "    2000-01-01 00:02:00   2.0\n",
      "    Freq: 30S, dtype: float64\n",
      "    \n",
      "    Upsample the series into 30 second bins and fill the ``NaN``\n",
      "    values using the ``pad`` method.\n",
      "    \n",
      "    >>> series.resample('30S').pad()[0:5]\n",
      "    2000-01-01 00:00:00    0\n",
      "    2000-01-01 00:00:30    0\n",
      "    2000-01-01 00:01:00    1\n",
      "    2000-01-01 00:01:30    1\n",
      "    2000-01-01 00:02:00    2\n",
      "    Freq: 30S, dtype: int64\n",
      "    \n",
      "    Upsample the series into 30 second bins and fill the\n",
      "    ``NaN`` values using the ``bfill`` method.\n",
      "    \n",
      "    >>> series.resample('30S').bfill()[0:5]\n",
      "    2000-01-01 00:00:00    0\n",
      "    2000-01-01 00:00:30    1\n",
      "    2000-01-01 00:01:00    1\n",
      "    2000-01-01 00:01:30    2\n",
      "    2000-01-01 00:02:00    2\n",
      "    Freq: 30S, dtype: int64\n",
      "    \n",
      "    Pass a custom function via ``apply``\n",
      "    \n",
      "    >>> def custom_resampler(array_like):\n",
      "    ...     return np.sum(array_like) + 5\n",
      "    ...\n",
      "    >>> series.resample('3T').apply(custom_resampler)\n",
      "    2000-01-01 00:00:00     8\n",
      "    2000-01-01 00:03:00    17\n",
      "    2000-01-01 00:06:00    26\n",
      "    Freq: 3T, dtype: int64\n",
      "    \n",
      "    For a Series with a PeriodIndex, the keyword `convention` can be\n",
      "    used to control whether to use the start or end of `rule`.\n",
      "    \n",
      "    Resample a year by quarter using 'start' `convention`. Values are\n",
      "    assigned to the first quarter of the period.\n",
      "    \n",
      "    >>> s = pd.Series([1, 2], index=pd.period_range('2012-01-01',\n",
      "    ...                                             freq='A',\n",
      "    ...                                             periods=2))\n",
      "    >>> s\n",
      "    2012    1\n",
      "    2013    2\n",
      "    Freq: A-DEC, dtype: int64\n",
      "    >>> s.resample('Q', convention='start').asfreq()\n",
      "    2012Q1    1.0\n",
      "    2012Q2    NaN\n",
      "    2012Q3    NaN\n",
      "    2012Q4    NaN\n",
      "    2013Q1    2.0\n",
      "    2013Q2    NaN\n",
      "    2013Q3    NaN\n",
      "    2013Q4    NaN\n",
      "    Freq: Q-DEC, dtype: float64\n",
      "    \n",
      "    Resample quarters by month using 'end' `convention`. Values are\n",
      "    assigned to the last month of the period.\n",
      "    \n",
      "    >>> q = pd.Series([1, 2, 3, 4], index=pd.period_range('2018-01-01',\n",
      "    ...                                                   freq='Q',\n",
      "    ...                                                   periods=4))\n",
      "    >>> q\n",
      "    2018Q1    1\n",
      "    2018Q2    2\n",
      "    2018Q3    3\n",
      "    2018Q4    4\n",
      "    Freq: Q-DEC, dtype: int64\n",
      "    >>> q.resample('M', convention='end').asfreq()\n",
      "    2018-03    1.0\n",
      "    2018-04    NaN\n",
      "    2018-05    NaN\n",
      "    2018-06    2.0\n",
      "    2018-07    NaN\n",
      "    2018-08    NaN\n",
      "    2018-09    3.0\n",
      "    2018-10    NaN\n",
      "    2018-11    NaN\n",
      "    2018-12    4.0\n",
      "    Freq: M, dtype: float64\n",
      "    \n",
      "    For DataFrame objects, the keyword `on` can be used to specify the\n",
      "    column instead of the index for resampling.\n",
      "    \n",
      "    >>> d = {'price': [10, 11, 9, 13, 14, 18, 17, 19],\n",
      "    ...      'volume': [50, 60, 40, 100, 50, 100, 40, 50]}\n",
      "    >>> df = pd.DataFrame(d)\n",
      "    >>> df['week_starting'] = pd.date_range('01/01/2018',\n",
      "    ...                                     periods=8,\n",
      "    ...                                     freq='W')\n",
      "    >>> df\n",
      "       price  volume week_starting\n",
      "    0     10      50    2018-01-07\n",
      "    1     11      60    2018-01-14\n",
      "    2      9      40    2018-01-21\n",
      "    3     13     100    2018-01-28\n",
      "    4     14      50    2018-02-04\n",
      "    5     18     100    2018-02-11\n",
      "    6     17      40    2018-02-18\n",
      "    7     19      50    2018-02-25\n",
      "    >>> df.resample('M', on='week_starting').mean()\n",
      "                   price  volume\n",
      "    week_starting\n",
      "    2018-01-31     10.75    62.5\n",
      "    2018-02-28     17.00    60.0\n",
      "    \n",
      "    For a DataFrame with MultiIndex, the keyword `level` can be used to\n",
      "    specify on which level the resampling needs to take place.\n",
      "    \n",
      "    >>> days = pd.date_range('1/1/2000', periods=4, freq='D')\n",
      "    >>> d2 = {'price': [10, 11, 9, 13, 14, 18, 17, 19],\n",
      "    ...       'volume': [50, 60, 40, 100, 50, 100, 40, 50]}\n",
      "    >>> df2 = pd.DataFrame(d2,\n",
      "    ...                    index=pd.MultiIndex.from_product([days,\n",
      "    ...                                                     ['morning',\n",
      "    ...                                                      'afternoon']]\n",
      "    ...                                                     ))\n",
      "    >>> df2\n",
      "                          price  volume\n",
      "    2000-01-01 morning       10      50\n",
      "               afternoon     11      60\n",
      "    2000-01-02 morning        9      40\n",
      "               afternoon     13     100\n",
      "    2000-01-03 morning       14      50\n",
      "               afternoon     18     100\n",
      "    2000-01-04 morning       17      40\n",
      "               afternoon     19      50\n",
      "    >>> df2.resample('D', level=0).sum()\n",
      "                price  volume\n",
      "    2000-01-01     21     110\n",
      "    2000-01-02     22     140\n",
      "    2000-01-03     32     150\n",
      "    2000-01-04     36      90\n",
      "    \n",
      "    If you want to adjust the start of the bins based on a fixed timestamp:\n",
      "    \n",
      "    >>> start, end = '2000-10-01 23:30:00', '2000-10-02 00:30:00'\n",
      "    >>> rng = pd.date_range(start, end, freq='7min')\n",
      "    >>> ts = pd.Series(np.arange(len(rng)) * 3, index=rng)\n",
      "    >>> ts\n",
      "    2000-10-01 23:30:00     0\n",
      "    2000-10-01 23:37:00     3\n",
      "    2000-10-01 23:44:00     6\n",
      "    2000-10-01 23:51:00     9\n",
      "    2000-10-01 23:58:00    12\n",
      "    2000-10-02 00:05:00    15\n",
      "    2000-10-02 00:12:00    18\n",
      "    2000-10-02 00:19:00    21\n",
      "    2000-10-02 00:26:00    24\n",
      "    Freq: 7T, dtype: int64\n",
      "    \n",
      "    >>> ts.resample('17min').sum()\n",
      "    2000-10-01 23:14:00     0\n",
      "    2000-10-01 23:31:00     9\n",
      "    2000-10-01 23:48:00    21\n",
      "    2000-10-02 00:05:00    54\n",
      "    2000-10-02 00:22:00    24\n",
      "    Freq: 17T, dtype: int64\n",
      "    \n",
      "    >>> ts.resample('17min', origin='epoch').sum()\n",
      "    2000-10-01 23:18:00     0\n",
      "    2000-10-01 23:35:00    18\n",
      "    2000-10-01 23:52:00    27\n",
      "    2000-10-02 00:09:00    39\n",
      "    2000-10-02 00:26:00    24\n",
      "    Freq: 17T, dtype: int64\n",
      "    \n",
      "    >>> ts.resample('17min', origin='2000-01-01').sum()\n",
      "    2000-10-01 23:24:00     3\n",
      "    2000-10-01 23:41:00    15\n",
      "    2000-10-01 23:58:00    45\n",
      "    2000-10-02 00:15:00    45\n",
      "    Freq: 17T, dtype: int64\n",
      "    \n",
      "    If you want to adjust the start of the bins with an `offset` Timedelta, the two\n",
      "    following lines are equivalent:\n",
      "    \n",
      "    >>> ts.resample('17min', origin='start').sum()\n",
      "    2000-10-01 23:30:00     9\n",
      "    2000-10-01 23:47:00    21\n",
      "    2000-10-02 00:04:00    54\n",
      "    2000-10-02 00:21:00    24\n",
      "    Freq: 17T, dtype: int64\n",
      "    \n",
      "    >>> ts.resample('17min', offset='23h30min').sum()\n",
      "    2000-10-01 23:30:00     9\n",
      "    2000-10-01 23:47:00    21\n",
      "    2000-10-02 00:04:00    54\n",
      "    2000-10-02 00:21:00    24\n",
      "    Freq: 17T, dtype: int64\n",
      "    \n",
      "    To replace the use of the deprecated `base` argument, you can now use `offset`,\n",
      "    in this example it is equivalent to have `base=2`:\n",
      "    \n",
      "    >>> ts.resample('17min', offset='2min').sum()\n",
      "    2000-10-01 23:16:00     0\n",
      "    2000-10-01 23:33:00     9\n",
      "    2000-10-01 23:50:00    36\n",
      "    2000-10-02 00:07:00    39\n",
      "    2000-10-02 00:24:00    24\n",
      "    Freq: 17T, dtype: int64\n",
      "    \n",
      "    To replace the use of the deprecated `loffset` argument:\n",
      "    \n",
      "    >>> from pandas.tseries.frequencies import to_offset\n",
      "    >>> loffset = '19min'\n",
      "    >>> ts_out = ts.resample('17min').sum()\n",
      "    >>> ts_out.index = ts_out.index + to_offset(loffset)\n",
      "    >>> ts_out\n",
      "    2000-10-01 23:33:00     0\n",
      "    2000-10-01 23:50:00     9\n",
      "    2000-10-02 00:07:00    21\n",
      "    2000-10-02 00:24:00    54\n",
      "    2000-10-02 00:41:00    24\n",
      "    Freq: 17T, dtype: int64\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(pd.DataFrame.resample)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
